package com.ndp.fb.rdb.mongo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.WriteConcern;
import com.ndp.fb.constant.MongoConstants;
import com.ndp.fb.mongo.api.EcMarketingActionRecordMongoService;
import com.ndp.fb.mongo.model.Serializer.PropertyFilter;

import java.util.Date;
import java.util.Iterator;

/**
 * Created by jacky.cheng on 2016/6/7.
 */
@Service
public class EcMarketingActionRecordMongoServiceImpl extends BaseMongoServiceImpl implements EcMarketingActionRecordMongoService {
    @Override
    public String getTableName() {
        return "EcMarketingActionRecord";
    }

    @Override
    public JSONObject findById(String id) {
        DBObject q = new BasicDBObject();
        q.put("id", id);
        DBCollection dbCollection = mongoDBClient.getCollection(getTableName());
        DBObject dbObject = dbCollection.findOne(q);
        if(dbObject==null){
            return null;
        }
        return JSONObject.parseObject(JSONObject.toJSONString(dbObject));
    }

    @Override
    public Iterator<DBObject> findByIdAndDate(Long marketingId, Date start, Date end) {
        DBObject q = new BasicDBObject();
        q.put("marketingId", marketingId);
        //开始结束时间
        DBObject time = new BasicDBObject();
        time.put("$gte", start);
        time.put("$lt", end);
        q.put("optTime", time);
        return this.find(q);
    }

    @Override
    public void pushEcAdActionRecord(String optId, JSONObject record) {
        DBCollection dbCollection = mongoDBClient.getCollection(getTableName());
        DBObject query = new BasicDBObject();
        query.put("id", optId);
        DBObject update = new BasicDBObject();
        DBObject dbObject = (DBObject) com.mongodb.util.JSON.parse(JSONObject.toJSONString(record));
        update.put("$push", new BasicDBObject("adActionRecords",dbObject));
        dbCollection.update(query, update, true, false, WriteConcern.JOURNALED);
    }

    @Override
    public void updateEcAdActionRecord(String optId, String id, JSONObject update) {
        DBCollection dbCollection = mongoDBClient.getCollection(getTableName());
        DBObject query = new BasicDBObject();
        query.put("id", optId);
        query.put("adActionRecords.id", id);
        DBObject updateObj = new BasicDBObject();
        DBObject dbObject = (DBObject) com.mongodb.util.JSON.parse(JSONObject.toJSONString(update, new PropertyFilter()));
        for(String key:dbObject.keySet()){
            updateObj.put("adActionRecords.$."+key, dbObject.get(key));
        }
        dbCollection.update(query, new BasicDBObject("$set", updateObj), true, false, WriteConcern.JOURNALED);
    }

    @Override
    public JSONObject findEcAdActionRecord(String optId, String ecActionRecordId) {
        DBCollection dbCollection = mongoDBClient.getCollection(getTableName());
        DBObject query = new BasicDBObject();
        query.put("id", optId);
        DBObject dbObject = dbCollection.findOne(query);
        if(dbObject==null||!dbObject.containsField("adActionRecords")){
            return null;
        }
        JSONObject obj = JSONObject.parseObject(JSONObject.toJSONString(dbObject));
        JSONArray adActionRecords = obj.getJSONArray("adActionRecords");
        for(int i=0;i<adActionRecords.size();i++){
            if(ecActionRecordId.equals(adActionRecords.getJSONObject(i).getString("id"))){
                return adActionRecords.getJSONObject(i);
            }
        }
        return null;
    }

    @Override
    public void updateEcMarketingActionRecord(String optId, JSONObject update) {
        DBCollection dbCollection = mongoDBClient.getCollection(getTableName());
        DBObject query = new BasicDBObject();
        query.put("id", optId);
        DBObject dbObject = (DBObject) com.mongodb.util.JSON.parse(JSONObject.toJSONString(update, new PropertyFilter()));
        dbCollection.update(query, new BasicDBObject("$set", dbObject), true, false, WriteConcern.JOURNALED);

    }

    @Override
    public JSONArray getLogs(String marketingId) {
        JSONArray result = new JSONArray();
        DBCollection dbCollection = mongoDBClient.getCollection(getTableName());
        DBObject query = new BasicDBObject();
        query.put("marketingId", marketingId);
        DBCursor cursor = dbCollection.find(query).sort(new BasicDBObject("optTime",-1)).limit(2);
        if(cursor==null){
            return result;
        }
        while (cursor.hasNext()) {
            result.add(JSONObject.parse(JSONObject.toJSONString(cursor.next())));
        }
        return result;
    }

}
